package com.example.friend.service.user.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.common.core.Utils.ThreadLocalUtil;
import com.example.common.core.constants.Constants;
import com.example.common.core.domin.PageQueryDTO;
import com.example.common.core.domin.TableDataInfo;
import com.example.friend.domain.message.Message;
import com.example.friend.domain.message.vo.MessageTextVO;
import com.example.friend.manager.MessageCacheManager;
import com.example.friend.mapper.message.MessageMapper;
import com.example.friend.mapper.message.MessageTextMapper;
import com.example.friend.service.user.IUserMessageService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserMessageServiceImpl implements IUserMessageService {

    @Autowired
    private MessageCacheManager messageCacheManager;

    @Autowired
    private MessageTextMapper messageTextMapper;

    @Autowired
    private MessageMapper messageMapper;


    @Override
    public TableDataInfo list(PageQueryDTO dto) {
        //获取当前用户id
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        //从缓存中获取消息列表数据
        //获取列表大小
        Long total = messageCacheManager.getListSize(userId);
        List<MessageTextVO> messageTextVOList;
        if (total == null || total == 0) {
            //从数据库中进行查询
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            messageTextVOList = messageTextMapper.selectUserMsgList(userId);
            //刷新缓存
            messageCacheManager.refreshCache(userId);
            total = new PageInfo<>(messageTextVOList).getTotal();
        } else {
            messageTextVOList = messageCacheManager.getMsgTextVOList(dto, userId);
        }
        if (CollectionUtil.isEmpty(messageTextVOList)) {
            return TableDataInfo.empty();
        }
        return TableDataInfo.success(messageTextVOList, total);
    }

    @Override
    public boolean deleteMessage(Long textId) {
        //删除用户消息记录
        Long userId = ThreadLocalUtil.get(Constants.USER_ID, Long.class);
        //查询出对应的messageId
        Message message = messageMapper.selectOne(new LambdaQueryWrapper<Message>()
                .eq(Message::getTextId, textId)
                .eq(Message::getRecId,userId));
        if (message == null) {
            return true;
        }
        Long size = messageMapper.selectCount(new LambdaQueryWrapper<Message>()
                .eq(Message::getTextId, textId));
        //先删除对应的message
        messageMapper.deleteById(message.getMessageId());
        messageCacheManager.deleteMessage(userId,message.getMessageId());
        //判断当前消息是否为多人所有
        if(size <= 1) {
            //清除messageText
            messageTextMapper.deleteById(textId);
            return messageCacheManager.deleteMessageText(textId);
        }
        return true;
    }
}
